/**
 * Created with JetBrains WebStorm.
 * User: Steven
 * Date: 25-4-13
 * Time: 20:27
 * To change this template use File | Settings | File Templates.
 */

/**
 * Skill class. Description coming soon.
 *
 * @param skillType
 * @param parent
 * @constructor
 */
function Skill(skillType, parent)
{
    this.id = -1;                   // Unique Skill ID
    this.parent = parent;           // Parent Character
    this.baseLevel = 5;             // Base Skill level
    this.level = 0;                 // Leveling progress
    this.progressLevel = 0;         // Skill progress for the current Character level
    this.skillType = skillType;     // SkillType of this Skill
    this.skillTypeKey = null;       // SkillType key of this Skill (replacement for "indexOf" method for database sync)
    this.governedAttribute = null;
    this.checked = false;           // UI checkbox binding
}

Skill.prototype.toString = function()
{
    var returnStr = "";
    returnStr += "SkillType: " + this.skillType;
    returnStr += ", BaseLevel: " + this.baseLevel;
    returnStr += ", Level: " + this.level;
    returnStr += ", G. Attribute: " + this.governedAttribute;
    //returnStr += ", Parent: " + this.parent;
    returnStr += ", Calculated stat: " + this.CalculateStat();
    return returnStr;
};

Skill.prototype.calculateStat = function()
{
    var stat = this.baseLevel + this.level;
    stat += this.isMajor() ? this.parent.class.getMajorSkillModifier(this.skillType) : 0;
    stat += this.parent.class.specialization.getSkillModifier(this.skillType);
    stat += this.parent.race.getSkillModifier(this.skillType);
    return stat;
};

Skill.prototype.calculateProgressStat = function()
{
    return this.calculateStat() + this.progressLevel;
}

Skill.prototype.isMajor = function()
{
    return this.parent.class.hasMajorSkillType(this.skillType);
};

Skill.prototype.setGovernedAttribute = function(attribute)
{
    this.governedAttribute = attribute;
};

Skill.prototype.levelUp = function()
{
    // Determine whether this skill is going to be leveled up or not
    var isLevelingUp = this.progressLevel > 0;

    // Level up the skill
    this.level += this.progressLevel;
    this.progressLevel = 0;

    return isLevelingUp;
};

Skill.prototype.adjustLevel = function($value)
{
    this.level += $value;
};

Skill.prototype.adjustProgressLevel = function($value)
{
    this.progressLevel += $value;
    this.parent.verifyLevelProgress();
};

/**
 * Holds the Skills of a Character.
 *
 * @param parent
 * @constructor
 */
function SkillSet(parent)
{
    // Setup
    this.parent = parent;   // Parent Character
    this.skills = {};       // Hashmap for skills

    // Foreach through all SkillTypes and create the respective Skill objects
    var keys = Object.keys(SkillTypes);
    for (var i = 0; i < keys.length; i++) {
        // Fetch the SkillType
        var currentSkillType = SkillTypes[keys[i]];

        // Create the Skill
        var skill = new Skill(currentSkillType, this.parent);

        // Configure Skill ID and skillTypeKey for database synchronization purposes
        skill.id = i;
        skill.skillTypeKey = keys[i];

        // Add the Skill to the skills collection
        this.skills[currentSkillType] = skill;
    }
}

SkillSet.prototype.getSkill = function(skillType)
{
    return this.skills[skillType];
};

SkillSet.prototype.getSkillByString = function(skillTypeString)
{
    // Obtain the SkillType key, and then pass it to getSkill
    return this.getSkill(SkillTypes[skillTypeString]);
};

SkillSet.prototype.getSkills = function()
{
    var skillCol = [];
    var skillKeys = Object.keys(this.skills);
    for(var i = 0; i < skillKeys.length; i++)
    {
        skillCol.push(this.skills[skillKeys[i]]);
    }
    return skillCol;
};

/**
 * SkillTypes enumeration
 */
var SkillTypes =
{
    "blade":"Blade",
    "blunt":"Blunt",
    "handtohand":"Hand-To-Hand",
    "alchemy":"Alchemy",
    "conjuration":"Conjuration",
    "mysticism":"Mysticism",
    "alteration":"Alteration",
    "destruction":"Destruction",
    "restoration":"Restoration",
    "marksmanship":"Marksmanship",
    "security":"Security",
    "sneak":"Sneak",
    "acrobatics":"Acrobatics",
    "athletics":"Athletics",
    "lightarmor":"Light Armor",
    "armorer":"Armorer",
    "block":"Block",
    "heavyarmor":"Heavy Armor",
    "illusion":"Illusion",
    "mercantile":"Mercantile",
    "speechcraft":"Speechcraft"
};
Object.freeze(SkillTypes);